import store from "@/store";
import {
  IncomeStream,
  LandingAgency,
  Partners,
  Project
} from "@/interface/entity";
import { constObjs } from "@/const/const";
import { myDateFormat } from "@/functions/general";

//-----------------------incomestream--------------------
export const processIncomeStreamBeforeToDb = (
  incomeStream: IncomeStream,
  method = ""
): IncomeStream => {
  const form = JSON.parse(JSON.stringify(incomeStream)) as IncomeStream;

  // if now is adding a incomesteam
  if (method == "add") {
    // generate projectguid when adding a string
    form.projectGuid = store.state.selectedProject.projectGuid;
  }

  // format date
  form.splitAmountDate = myDateFormat(form.splitAmountDate);
  form.invoiceIssueDate = myDateFormat(form.invoiceIssueDate);
  form.createdDate = myDateFormat(form.createdDate);

  // to json string
  // todo client
  form.clients = JSON.stringify(form.clients as LandingAgency[]);
  form.landingAgencies = JSON.stringify(
    form.landingAgencies as LandingAgency[]
  );
  form.partners = JSON.stringify(form.partners as Partners[]);

  return form;
};

// generate incomestream list using the data from db
export const generateIncomeStreamLists = (
  incomeStreamData: IncomeStream[]
): IncomeStream[] => {
  // the data which will be return
  const data = JSON.parse(JSON.stringify(incomeStreamData)) as IncomeStream[];
  // todo client
  let maxLandingAgenciesIndex = 0,
    maxPartnersIndex = 0,
    maxLandingAgenciesLength = 0,
    maxPartnersLength = 0,
    maxClientsIndex = 0,
    maxClientsLength = 0;
  // generate the cols base on the incomestreams
  data.forEach((element, index) => {
    // 在显示table的时候记录更新前入账金额的值
    // 根据所属项目更新element的可选属性值

    // 所属项目,一定存在，projectGuid是不变的，根据这个来找
    const nowProject = store.state.projectList.find(
      project => project.projectGuid == element.projectGuid
    ) as Project;

    // landingAgencies and partners of this stream(project)
    // todo client
    if (index == 0) {
      console.log(nowProject.clients);
    }
    const clients = nowProject.clients as LandingAgency[];
    const landingAgencies = nowProject.landingAgencies as LandingAgency[];
    const partners = nowProject.partners as Partners[];

    // update the max
    // todo client
    if (clients.length > maxClientsLength) {
      maxClientsLength = clients.length;
      maxClientsIndex = index;
    }
    if (landingAgencies.length > maxLandingAgenciesLength) {
      maxLandingAgenciesLength = landingAgencies.length;
      maxLandingAgenciesIndex = index;
    }
    if (partners.length > maxPartnersLength) {
      maxPartnersLength = partners.length;
      maxPartnersIndex = index;
    }

    element.categories = nowProject.categories;
    element.projectName = nowProject.projectName;
    element.projectNum = nowProject.projectNum;
    element.schoolManageRadio = nowProject.schoolManageRadio;
    element.classPayRadio = nowProject.classPayRadio;
    element.devFundingRadio = nowProject.devFundingRadio;
    element.welfareRadio = nowProject.welfareRadio;

    // agencies
    // to object
    // todo client
    element.clients = JSON.parse(element.clients as string) as LandingAgency[];
    element.landingAgencies = JSON.parse(
      element.landingAgencies as string
    ) as LandingAgency[];
    element.partners = JSON.parse(element.partners as string) as Partners[];
  });

  // save the max
  constObjs.incomeStreamTablePattern.forEach(pattern => {
    if (pattern.prop == "clients") {
      pattern.maxLengthIndex = maxClientsIndex;
      pattern.maxSubItemNum = maxClientsLength;
    }
    if (pattern.prop == "landingAgencies") {
      pattern.maxLengthIndex = maxLandingAgenciesIndex;
      pattern.maxSubItemNum = maxLandingAgenciesLength;
    }
    if (pattern.prop == "partners") {
      pattern.maxLengthIndex = maxPartnersIndex;
      pattern.maxSubItemNum = maxPartnersLength;
    }
  });
  return data;
};
//-----------------------incomestream end-----------------
